package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/gorilla/websocket"
	"io/ioutil"
	"kikt.top/chat_server/model"
	"net/http"
)

var upgrade = websocket.Upgrader{
	CheckOrigin: func(r *http.Request) bool {
		return true
	},
}

func main() {
	g := gin.Default()
	g.GET("/ping", ping)
	g.POST("/login", login)

	g.Run(":10000")
}

func login(c *gin.Context) {
	bytes, _ := ioutil.ReadAll(c.Request.Body)
	request := model.LoginRequest{}
	request.XXX_Unmarshal(bytes)

	if request.Username == "user1" || request.Pwd == "1234" {
		response := &model.LoginResponse{}
		info := model.LoginInfo{}
		info.Uid = "1"
		info.HeaderUrl = "http://"
		response.Data = &info
	}
}

func ping(c *gin.Context) {
	fmt.Println("接到连接")
	ws, err := upgrade.Upgrade(c.Writer, c.Request, nil)
	if err != nil {
		return
	}

	defer ws.Close()

	for {
		messageType, bytes, err := ws.ReadMessage()

		if err != nil {
			fmt.Println("err = ", err)
			break
		}

		fmt.Printf("msgType = %d, bytes = %s\n", messageType, string(bytes))
	}
}
